package org.ph.share.lock.test.thread;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * Thread 测试类
 * 线程的六大状态 这是对应状态的枚举{@link  java.lang.Thread.State}
 * 这里将各个子线程设置为守护线程，随着主线程的结束而结束
 * @author Hush.
 * @since 2023/5/15 9:53
 */
@Slf4j
public class ThreadTest1_1 {
    public static void main(String[] args) throws InterruptedException {

        // NEW
        Thread thread1 = new Thread("t1") {
            @Override
            public void run() {

            }
        };

        // RUNNABLE
        Thread thread2 = new Thread("t2") {
            @Override
            public void run() {
                synchronized (ThreadTest1_1.class) {
                    while (true) {

                    }
                }

            }
        };
        // 设置为守护线程
        thread2.setDaemon(true);
        thread2.start();

        // BLOCKED
        Thread thread3 = new Thread("t3") {
            @Override
            public void run() {
                synchronized (ThreadTest1_1.class) {
                    log.info("BLOCKED");
                }
            }
        };
        thread3.setDaemon(true);
        thread3.start();

        // WAITING
        Thread thread4 = new Thread("t4") {
            @SneakyThrows
            @Override
            public void run() {
                thread2.join();
            }
        };
        thread4.setDaemon(true);
        thread4.start();

        // TIMED_WAITING
        Thread thread5 = new Thread("t5") {
            @SneakyThrows
            @Override
            public void run() {
                Thread.sleep(10000);
            }
        };
        thread5.start();

        // TERMINATED
        Thread thread6 = new Thread("t6") {
            @Override
            public void run() {

            }
        };
        thread6.start();


        TimeUnit.SECONDS.sleep(2);
        log.info(thread1.getState().name());
        log.info(thread2.getState().name());
        log.info(thread3.getState().name());
        log.info(thread4.getState().name());
        log.info(thread5.getState().name());
        log.info(thread6.getState().name());

    }
}
